package org.crud4j.validator;

import java.util.ArrayList;
import java.util.Collection;

public class BeanValidatorException extends Exception {

	private static final long serialVersionUID = 1L;

	private Collection<ValidationResult> validationResults;

	public BeanValidatorException(Object bean, ValidationResult validationResult) {
		super(createFullMessage(bean, createMessage(validationResult)));
		this.validationResults = new ArrayList<ValidationResult>();
		this.validationResults.add(validationResult);
	}

	public BeanValidatorException(Object bean,
			Collection<ValidationResult> validationResults) {
		super(createFullMessage(bean, createMessage(validationResults)));
		this.validationResults = validationResults;
	}

	public Collection<ValidationResult> getValidationResults() {
		return validationResults;
	}

	private static String createFullMessage(Object bean, String message) {
		return message + bean.getClass().getName() + "\n" + bean.toString()
				+ "\n";
	}

	private static String createMessage(ValidationResult validationResult) {
		return validationResult.getName() + ": "
				+ validationResult.getValueValidatorException().getMessage();
	}

	private static String createMessage(
			Collection<ValidationResult> validationResults) {
		String message = "";
		for (ValidationResult validationResult : validationResults) {
			message += createMessage(validationResult) + "\n";
		}
		return message;

	}

}
